﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Vikipedia.CommonTools.Subtitles
{
    public class SrtSubtitleReader : SubtitleReader
    {


        SubtitleItem<String> CreateItem(List<String> inputls)
        {
            var itm = new SubtitleItem<String>();

            //读取第一行序号
            int i = 0;
            int.TryParse(inputls[0], out i);
            itm.IndexIfAvsilable = i;


            //读取第二行时间

            var strs = inputls[1].Replace(',', '.').Split(new string[] { "-->" }, StringSplitOptions.None);
            itm.Position = TimeSpan.Parse(strs[0]);
            itm.Duration = TimeSpan.Parse(strs[1]).Add(itm.Position.Negate());


            //读取其他行内容
            itm.Data = string.Join("\r\n", inputls.Skip(2).ToArray());
            return itm;
        }
        internal IEnumerable<List<String>> GetSubtitleItemStrings(FileInfo file)
        {
            using (var sr = new System.IO.StreamReader(file.OpenRead(), IOC.Unity.Container.Resolve<Encoding>("Current")))
            {
                List<String> ls = new List<string>();
                while (!sr.EndOfStream)
                {
                    var s = sr.ReadLine();
                    if (s.Trim().Length == 0)
                    {
                        var rval = ls;
                        ls = new List<string>();
                        yield return rval;
                    }
                    else ls.Add(s);
                }

            }


        }



        public SrtSubtitleReader()
        {

        }
        public override IEnumerable<ISubtitleItem> GetSubtitleItems(FileInfo file)
        {
            foreach (var ib in GetSubtitleItemStrings(file))
            {

                yield return CreateItem(ib);
            }


        }
        public override bool IsFormatMatch(FileInfo file)
        {
            using (var stm = file.Open( FileMode.Open , FileAccess.Read , FileShare.Read     ))
            {
                var sr = new StreamReader(stm,  true );
                for (int i = 1; i < 5; i++)
                {
                    string tmp;
                    tmp = sr.ReadLine();  //Index
                    int idx;
                    if (int.TryParse(tmp, out idx)) //Index must ==i
                    {
                        if (idx != i) return false;
                    }
                    else return false;//index must be int

                    tmp = sr.ReadLine();
                    if (tmp.IndexOf("-->") == -1) return false;  //Time Line
                    tmp = sr.ReadLine(); //Text
                    tmp = sr.ReadLine();//Blank Line
                    while (tmp.Trim().Length != 0) tmp = sr.ReadLine();

                }
            };
            return true;
        }
        public override string Name
        {
            get
            {
                return "SrtSubtitleReader";
            }

        }
    }
}
